/* 
 *  Copyright (c) 2011—2013 Panguso.com. All rights reserved. 
 *  File Name:  XESConvertorMonitor_1.java                                      
 *  File Contents Outline :  XES转换监视器
 *  File  Create Date:   2012年7月30日
 *  File Version#: Revision#: #1                  
 *  File  Latest Modify Date : 2013年1月16日
 *  File Author :  bachuan@panguso.com
 */
package cn.edu.thu.log.xes;

import java.util.Date;
import java.util.Hashtable;

import org.deckfour.xes.model.XTrace;

import cn.edu.thu.log.read.LogBuffer;
import cn.edu.thu.log.util.Format;
import cn.edu.thu.log.util.PanGuConstants;

public class XESConvertorMonitor_1 {

    private static XESConvertorMonitor singleton = null;

    public synchronized static XESConvertorMonitor instance() {
        if (singleton == null) {
            singleton = new XESConvertorMonitor();
        }
        return singleton;
    }
    public int numOfEvent;
   // private ArrayList<String> fitList;
 //   private Hashtable<XTrace, Integer> fitTraceMap;
    /**
     * map of laster arrival time in one case
     */
    private Hashtable<XTrace, String> lastestArrivalMap;

    /**
     * map of earliest arrival time . one case
     */
    //private Hashtable<XTrace, String> earliestArrivalMap;
    public XESConvertorMonitor_1() {
        this.numOfEvent = 0;
        this.lastestArrivalMap = new Hashtable<XTrace, String>();
//        this.fitTraceMap = new Hashtable<XTrace, Integer>();
      //  this.fitList = new ArrayList<String>();
//        fitList.add("vstar.panguso.com");
//        fitList.add("my.panguso.com/reg.jsp");
        //this.earliestArrivalMap = new Hashtable<XTrace, String>();
    }

    public boolean ifExceedMaxEventNum() {
        if (numOfEvent > PanGuConstants.MAX_EVENT_NUM) {
            return true;
        }
        return false;
    }

    public void addEventNum(int addSize) {
        numOfEvent = numOfEvent + addSize;
    }

    /**
     * check if the logBuffer arriveTime is within the time limit of one case
     *
     * @param logBuffer arrived logBuffer
     * @param caseIDValue the case's caseID
     * @return true:logBuffer attive within case; false: logBuffer timeout
     */
    public boolean ifTimeOut(LogBuffer logBuffer, XTrace trace) {
        boolean ifTimeOut = false;
        String lasterTimeString = getLastestArrivalMap().get(trace);
        String arriveTimeString = logBuffer.getTimeStamp();
        Date lasterTime = Format.StringToTimeStamp(lasterTimeString);
        Date arriveTime = Format.StringToTimeStamp(arriveTimeString);
        if (arriveTime.after(lasterTime)) {
            if (Math.abs(arriveTime.getTime() - lasterTime.getTime()) > PanGuConstants.TIMEROUT) {
                ifTimeOut = true;
            }
        }
        return ifTimeOut;
    }

//    public int ifFit(String activity) {
//        int ifFit = 0;//not fit
//        for (int i = 0; i < fitList.size(); i++) {
//            if (activity.contains(fitList.get(i))) {
//                ifFit = 1;//fit
//                //test
//               
//            }
//        }
//
//        return ifFit;
//
//    }
//    
//    public String trimAvtivity(String beforeTrim) {
//        String afterTrim=beforeTrim;
//         if (beforeTrim.contains("?st=")) {
//                    int index = beforeTrim.indexOf("?st=");
//                    afterTrim = beforeTrim.substring(0, index);
//                   //  System.out.println("activity after:"+afterTrim);
//                }
//         return afterTrim;
//    }
    

    public void updateLastestArrivalMap(XTrace trace,
            String timeStamp) {
        getLastestArrivalMap().put(trace, timeStamp);
    }

    /**
     * @return the lastestArrivalMap
     */
    public Hashtable<XTrace, String> getLastestArrivalMap() {
        return lastestArrivalMap;
    }

    /**
     * @param lastestArrivalMap the lastestArrivalMap to set
     */
    public void setLastestArrivalMap(Hashtable<XTrace, String> lastestArrivalMap) {
        this.lastestArrivalMap = lastestArrivalMap;
    }

//    /**
//     * @return the fitList
//     */
//    public ArrayList<String> getFitList() {
//        return fitList;
//    }
//
//    /**
//     * @param fitList the fitList to set
//     */
//    public void setFitList(ArrayList<String> fitList) {
//        this.fitList = fitList;
//    }
//
//    /**
//     * @return the fitTraceMap
//     */
//    public Hashtable<XTrace, Integer> getFitTraceMap() {
//        return fitTraceMap;
//    }
//
//    /**
//     * @param fitTraceMap the fitTraceMap to set
//     */
//    public void setFitTraceMap(Hashtable<XTrace, Integer> fitTraceMap) {
//        this.fitTraceMap = fitTraceMap;
//    }

//    public void deleteNotFit(XLog log) {
//        ArrayList<XTrace> deleteTraceList = new ArrayList<XTrace>();
//        Iterator iterator = log.iterator();
//        while (iterator.hasNext()) {
//            XTrace trace = (XTrace) iterator.next();
//            if (getFitTraceMap().get(trace) == 0) {
//                deleteTraceList.add(trace);
//                getFitTraceMap().remove(trace);
//            }
//
//        }
//        log.removeAll(deleteTraceList);
//    }
}
